
package ProOF.apl.problems.real.operators;

import ProOF.apl.problems.real.RealCodification;
import ProOF.apl.problems.real.RealProblem;
import ProOF.com.LinkerNodes;
import ProOF.com.LinkerParameters;
import ProOF.gen.best.BestSol;
import ProOF.gen.operator.oInitializer;
import ProOF.opt.abst.problem.meta.Solution;
import ProOF.utilities.uUtil;

/**
 *
 * @author Hossomi
 */
public class RealInitAnneal extends oInitializer<RealProblem, RealCodification> {
    private BestSol best;
    private double alfa;
    private double search;
    
    @Override
    public String name() {
        return "Annealing";
    }

    @Override
    public void services(LinkerNodes com) throws Exception {
        best = com.need(BestSol.class, best);
    }
    
    @Override
    public void parameters(LinkerParameters win) throws Exception {
        alfa = win.Dbl("Annealing factor", 0.0001, 0, 1);
    }

    @Override
    public void start() throws Exception {
        super.start();
        search = 1.0;
    }
    
    @Override
    public void initialize(RealProblem prob, RealCodification ind) throws Exception {
        Solution sol = best.ind();
        RealCodification bestInd;
        double range;
        
        if (sol == null) {
            for (int i = 0; i < prob.func.dimension(); i++) {
                ind.x[i] = prob.rmd.nextDouble(prob.func.min(i), prob.func.max(i));
            }
        }
        else {
            bestInd = (RealCodification) sol.codif();
            for (int i = 0; i < prob.func.dimension(); i++) {
                range = (prob.func.max(i) - prob.func.min(i))*search/2;
                ind.x[i] = bestInd.x[i] + prob.rmd.nextDouble(-range, range);
            }
            
            prob.func.adjust(ind.x);
            search *= (1 - alfa);
        }
    }

}
